**************************************************************
*HURRICANES AND GAS GOUGING - MAIN PAPER AND APPENDIX STATION-BY-STATION EFFECTS
**************************************************************
frame copy default station, replace
frame change station
keep if sample_main==1
gsort station_id date

****************************
*STATION-BY-STATION MARKUP REGRESSIONS	   
keep station_id retail wholesale pre_hur hur post_hur CTST CHRT CTSN CHRN temp temp2 state_name year month dow county_FIPS storm_window	  
tab station_id, gen(st_id_)

*Creating Variables 
gen station=.	
gen pre_hur_beta1=.
gen hur_beta1=.
gen post_hur_beta1=.
gen n_obs1=.

gen pre_hur_beta2=.
gen hur_beta2=.
gen post_hur_beta2=.
gen n_obs2=.
  
gen pre_hur_beta3=.
gen hur_beta3=.
gen post_hur_beta3=.
gen n_obs3=.
gen markup_st=.

*Estimating Station-level Hurricane Impacts (Markups)
forvalues i=1/4663 {
	di `i'
	qui sum station_id if st_id_`i'==1
	replace station= r(mean)  in `i'
	
	qui capture noisily: reg retail pre_hur hur post_hur wholesale i.year i.month if st_id_`i'==1
	capture noisily replace pre_hur_beta1=_b[pre_hur] in `i'
	capture noisily replace hur_beta1=_b[hur] in `i'
	capture noisily replace post_hur_beta1=_b[post_hur] in `i'
	capture noisily replace n_obs1=e(N) in `i'	
	
	qui capture noisily: reghdfe retail wholesale pre_hur hur post_hur CTST CHRT CTSN CHRN temp temp2 if st_id_`i'==1, absorb(year month dow)	
	capture noisily replace pre_hur_beta2=_b[pre_hur] in `i'
	capture noisily replace hur_beta2=_b[hur] in `i'
	capture noisily replace post_hur_beta2=_b[post_hur] in `i'
	capture noisily replace n_obs2=e(N) in `i'	
	
	qui capture noisily: reg retail wholesale pre_hur hur post_hur if st_id_`i'==1
	capture noisily replace pre_hur_beta3=_b[pre_hur] in `i'
	capture noisily replace hur_beta3=_b[hur] in `i'
	capture noisily replace post_hur_beta3=_b[post_hur] in `i'
	capture noisily replace n_obs3=e(N) in `i'	
	capture noisily replace markup_st=_b[_cons] in `i'	
		
}
*
keep station-markup_st
drop if missing(station)
save $data_clean/station_effects_$outputdate, replace

****************************
*CENSUS TRACT AREA (MILES SQUARED)
frame create census_temp
frame change census_temp

*Florida
import delimited "$data_census\Florida.csv",  clear
keep geoid10 aland10
rename aland10 land_area
replace land_area=land_area/2589988.1103 //1 Square Mile =2589988.1103 Square Meters
save $temp\tract_size, replace 

*Louisiana
import delimited "$data_census\Louisiana.csv",  clear
keep geoid10 aland10
rename aland10 land_area
replace land_area=land_area/2589988.1103 //1 Square Mile =2589988.1103 Square Meters
append using $temp\tract_size    

tostring geoid10, gen(tract_FIPS) format(%17.0g) force
save $temp\tract_size, replace 

frame change default
frame drop census_temp


****************************
*STATION EFFECTS CORRELATIONS
use $data_davis\gouging_clean_20191124, clear
keep if sample_main==1
keep station_id name rt_lat rt_lng brand_maj-within_10km_df state_code-MailingZip st_name-nearRack3_st hw_nearest_dist
gduplicates drop
*
rename station_id station
merge 1:1 station using $data_clean/station_effects_20200623.dta
drop _merge
*
merge m:1 tract_FIPS using $temp\tract_size
drop if _merge==2
drop _merge
*
*Labeling
label var pre_hur_beta1 "Pre-Hurricane Markup (Model 1)"
label var hur_beta1 "Hurricane Markup (Model 1)"
label var post_hur_beta1 "Post-Hurricane Markup (Model 1)"
label var pre_hur_beta2 "Pre-Hurricane Markup (Model 2)"
label var hur_beta2 "Hurricane Markup (Model 2)"
label var post_hur_beta2 "Post-Hurricane Markup (Model 2)"
label var pre_hur_beta3 "Pre-Hurricane Markup (No Controls)"
label var hur_beta3 "Hurricane Markup (No Controls)"
label var post_hur_beta3 "Post-Hurricane Markup (No Controls)"
label var markup_st "Station Baseline Markup (No Controls)"
*
*Updating values (Recorded as 0 if coulnd't estimate)
replace pre_hur_beta1=. if pre_hur_beta1==0|hur_beta1==0|post_hur_beta1==0
replace hur_beta1=. if missing(pre_hur_beta1)|hur_beta1==0|post_hur_beta1==0
replace post_hur_beta1=. if missing(pre_hur_beta1)|missing(hur_beta1)|post_hur_beta1==0
replace pre_hur_beta2=. if pre_hur_beta2==0|hur_beta2==0|post_hur_beta2==0
replace hur_beta2=. if missing(pre_hur_beta2)|hur_beta2==0|post_hur_beta2==0
replace post_hur_beta2=. if missing(pre_hur_beta2)|missing(hur_beta2)|post_hur_beta2==0
replace pre_hur_beta3=. if pre_hur_beta3==0|hur_beta3==0|post_hur_beta3==0
replace hur_beta3=. if missing(pre_hur_beta3)|hur_beta3==0|post_hur_beta3==0
replace post_hur_beta3=. if missing(pre_hur_beta3)|missing(hur_beta3)|post_hur_beta3==0
*
*Population density
gen pop_den = pop/land_area
*
*
*Summarizing Markup Distributions Across Models
sum  pre_hur_beta1 hur_beta1 post_hur_beta1
*
*Calculating percent if markups >=0.2
gen beta_post_hi0=0
	replace beta_post_hi0=1 if post_hur_beta1>=0.2 & !missing(post_hur_beta1)
	replace beta_post_hi0=sum(beta_post_hi)
egen beta_post_hi=max(beta_post_hi0)
	replace beta_post_hi=beta_post_hi/2770 //Fraction above 0.10 post-hurricane excess markup
sum beta_post_hi
drop beta_post_hi0

********************************************************************************
*FIGURE 1: POST-HURRICANE MARKUP HETEROGENEITY
********************************************************************************
twoway (hist post_hur_beta1, lc(edkblue*0.4)  xline(-0.028, lc(cranberry)) fcolor(none))  ///
		(pcarrowi 1.5 0.25 0.75 0.22 (3) "4.2% with >$0.20/gal margin" ///
		5.2 0.05  5 -0.02 (3) "-$0.028/gal average margin", color(black))  ///
      (kdensity post_hur_beta1, lc(edkblue)  ),   ///
	   graphregion(color(white)) bgcolor(white) ///
	   ytitle("Density") ///  
	  xtitle("Post-Hurricane Margins")  legend(off) 
graph export $figs\post-hur-hist-$outputdate.png, replace	
   
   
********************************************************************************
*FIGURE B.8: STATION POST-HURRICANE MARKUPS AND BASELINE MARKUPS
********************************************************************************
corr post_hur_beta1 markup_st
twoway lfit post_hur_beta1 markup_st, lwidth(0.6) lcolor(black) || ///
       scatter post_hur_beta1 markup_st, ///
       m(circle) mc(edkblue) msize(vsmall)  ///
	   graphregion(color(white)) bgcolor(white) ///
	   xtitle("Baseline Markups") ///  
	   ytitle("Post-Hurricane Margins")  legend(off) ///
	   text(0.5 0.9 "Correlation Coefficient=0.199", place(e) size(small))
graph export $figs\post-hur-basemarkup-$outputdate.png, replace	


********************************************************************************
*FIGURE B.9: STATIONS POST-HURRICANE MARKUPS AND LOCAL SUPPLY AND DEMAND CONDITIONS
********************************************************************************
  
*****************
*PANEL (A): LOCAL COMPETITION
corr post_hur_beta1 within_1km_df
twoway lfit post_hur_beta1 within_1km_df, lwidth(0.6) lcolor(black) || ///
       scatter post_hur_beta1 within_1km_df, ///
       m(circle) mc(edkblue) msize(vsmall)  ///
	   graphregion(color(white)) bgcolor(white) ///
	   xtitle("Branded Major") ///  
	   ytitle("Post-Hurricane Margins")  legend(off)  ///
	   text(0.5  5 "Correlation Coefficient=0.076", place(e) size(small))
graph export $figs\post-hur-competitors-$outputdate.png, replace	   
	   
*****************
*PANEL (B): BRANDED VERSUS UNBRANDED STATIONS
corr post_hur_beta1 brand_maj
twoway lfit post_hur_beta1 brand_maj, lwidth(0.6) lcolor(black) || ///
       scatter post_hur_beta1 brand_maj, ///
       m(circle) mc(edkblue) msize(vsmall)  ///
	   graphregion(color(white)) bgcolor(white) ///
	   xtitle("Branded Major") ///  
	   ytitle("Post-Hurricane Margins")  legend(off)  ///
	   text(0.5 0.1 "Correlation Coefficient=0.1572", place(e) size(small))
graph export $figs\post-hur-brand-$outputdate.png, replace	
 	
*****************
*PANEL (C): CENSUS TRACT POPULATION
corr post_hur_beta1 pop
twoway lfit post_hur_beta1 pop, lwidth(0.6) lcolor(black) || ///
       scatter post_hur_beta1 pop, ///
       m(circle) mc(edkblue) msize(vsmall)  ///
	   graphregion(color(white)) bgcolor(white) ///
	   xtitle("Population") ///  
	   ytitle("Post-Hurricane Margins")  legend(off) ///
	   text(0.5 10000 "Correlation Coefficient=0.0824", place(e) size(small))
graph export $figs\post-hur-population-$outputdate.png, replace	

*****************
*PANEL (D): CENSUS TRACT MEDIAN HOUSEHOLD INCOME
corr post_hur_beta1 income_hh_median
twoway lfit post_hur_beta1 income_hh_median, lwidth(0.6) lcolor(black) || ///
       scatter post_hur_beta1 income_hh_median, ///
       m(circle) mc(edkblue) msize(vsmall)  ///
	   graphregion(color(white)) bgcolor(white) ///
	   xtitle("Median HH Income") ///  
	   ytitle("Post-Hurricane Margins")  legend(off) ///
	   text(0.35 90000 "Correlation Coefficient=0.045", place(e) size(small))
graph export $figs\post-hur-income-$outputdate.png, replace	


********************************************************************************
*TABLE B.1: STATIONS POST-HURRICANE MARKUPS AND SUPPLY AND DEMAND FACTORS
********************************************************************************	   	    
eststo clear
encode state_FIPS, gen(st)
replace pop=pop/1000 //Thousand people
replace housing=housing/1000 //Thousand houses
replace hw_nearest_dist= hw_nearest_dist/1000 //KM to highway 
replace income_hh_median=income_hh_median/10000 //Thousand dollars
gen pop_nonwhite=(100-pop_white)/100 //% non-white
gen educ_higher=(edu_associate+edu_bachelor+edu_gradover)/100 //%associates degree or higher
gen unemp=empl_cvlbr_unemp/100 //% civilian labor force unemployed
gen cons=1	
		
*Column 1 - State FE
eststo: reghdfe post_hur_beta1 brand_maj within_1km_df pop income_hh_median , vce(robust) absorb(state_FIPS)
	estadd local state_fe "Yes"
	estadd local county_fe "No"
est store A
			   
*Column 2 - County FE
eststo: reghdfe post_hur_beta1 brand_maj within_1km_df pop income_hh_median , vce(robust) absorb(county_FIPS)
	estadd local state_fe "No"
	estadd local county_fe "Yes"
est store B
			   
*Column 3 - State FE
eststo: reghdfe  post_hur_beta1 brand_maj ret_maj within_1km_df km_to_nearRack1 hw_nearest_dist ///
        pop pop_nonwhite educ_higher unemp housing housing_vacant ///
		income_hh_median, vce(robust) absorb(state_FIPS)
	estadd local state_fe "Yes"
	estadd local county_fe "No"
est store C
			   
*Column 4 - County FE
eststo: reghdfe  post_hur_beta1 brand_maj ret_maj within_1km_df km_to_nearRack1 hw_nearest_dist ///
        pop pop_nonwhite educ_higher unemp housing housing_vacant ///
		income_hh_median, vce(robust) absorb(county_FIPS)
	estadd local state_fe "No"
	estadd local county_fe "Yes"
est store D

esttab A B C D using "$output/station-markup-het-$outputdate.csv", replace label ///
     b(a2) nonumber ///
    starlevels(* 0.10 ** 0.05 *** 0.01) ///
	title(Station Post-Hurricane Markups and Supply/Demand Factors)  ///
	cells(b(fmt(3) star) se(fmt(3) par)) ///
	drop(_cons) ///
	note(Notes: The dependent variable is the estimated station-specific, post-hurricane ///
	     markup. All demographic data are at the Census tract level and are from the 2010 ///
		 Census. *, **, and ***   denote significance at the 10\%, 5\%, and 1\% level.) ///
	coef(brand_maj "Branded Major" ret_maj "Retail Major" within_1km_df "Competitors w/in 1KM"  ///
		 km_to_nearRack1 "KM to Rack" hw_nearest_dist "KM to Highway" pop "Population (1000 people)" ///
		 pop_nonwhite "Non-White Population (%)" educ_higher "Associates Degree or Higher (%)" unemp "Unemployment (%)" ///
		 housing "Housing (1000 units)" housing_vacant "Vacant Housing (%)" income_hh_median "Median Income ($10K)") ///
	scalars("state_fe State FE" "county_fe County FE" )  ///
			 sfmt(%8.0f) mlabels((1) (2) (3) (4)) collabels(none) 
frame change default